﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Forum;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Forum
{
    public partial class FocusList : PortalPage
    {

        /// <summary>
        /// 排序方式
        /// </summary>
        public SysEnum.ForumOrderby ForumOrderby
        {
            get
            {
                try
                {
                    var id = Request.QueryString["ForumOrderby"];
                    SysEnum.ForumOrderby type = (SysEnum.ForumOrderby)Enum.Parse(typeof(SysEnum.ForumOrderby), id);
                    return type;
                }
                catch (Exception)
                {

                    return SysEnum.ForumOrderby.NewForum;
                }
            }
        }
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        /// <summary>
        /// 查询文本
        /// </summary>
        private string QueryText
        {
            get
            {
                return this.Request["QueryText"] ?? "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();
            }
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            this.hdForumOrderby.Value = ForumOrderby.ToString();

        }

        /// <summary>
        /// 返回数据
        /// </summary>
        private void WriteList()
        {
            var pageIndex = ValidateHelper.IsInt(this.PageIndex) ? Convert.ToInt32(this.PageIndex) : 0;
            var pageSize = ValidateHelper.IsInt(this.PageSize) ? Convert.ToInt32(this.PageSize) : 20;
            DataTable dt = PartyForum.GetGridData(pageIndex, pageSize, this.QueryText, "", SysEnum.ForumOrderby.Focus,false).Tables[0];
            dt.Columns.Add("ImageIco", typeof(string));
            dt.Columns.Add("CreateTimeEx", typeof(string));
            dt.Columns.Add("ReplyTimeEx", typeof(string));
            dt.Columns.Add("IsFirstName", typeof(string));
            dt.Columns["ImageIco"].DefaultValue = "../../Resource/Themes/Default/Images/Widget/bbs_folder.gif";
            foreach (DataRow dr in dt.Rows)
            {

                dr["IsFirstName"] = ReturnTitle(dr["IsFirst"]);
                dr["ImageIco"] = GetImageUrl(dr["IsEssence"]);
                dr["CreateTimeEx"] = Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss");
                try
                {
                    dr["ReplyTimeEx"] = Convert.ToDateTime(dr["ReplyTime"]).ToString("yyyy-MM-dd HH:mm:ss");
                }
                catch (Exception)
                {
                    dr["ReplyTimeEx"] = "";
                }
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        /// <summary>
        /// 返回总数
        /// </summary>
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyForum.GetGridData(0, 1, this.QueryText, "", SysEnum.ForumOrderby.Focus, false).Tables[1].Rows[0][0].ToString());
            Response.End();
        }
        public string GetImageUrl(object o)
        {
            if (o != DBNull.Value && o != null && Convert.ToString(o) == "1")
                return "../../Resource/Themes/Default/Images/Widget/bbs_folder_jin.gif";
            else
                return "../../Resource/Themes/Default/Images/Widget/bbs_folder.gif";
        }
        public string ReturnTitle(object isFirst)
        {
            string str = "";
            if (isFirst != DBNull.Value && isFirst != null && Convert.ToInt32(isFirst) == 1)
                str = "[置顶]" + str;

            return str;
        }
    }
}